#!/bin/bash
#
# Bring up/down opensm
#
# chkconfig: - 15 85
# description: Activates/Deactivates InfiniBand Subnet Manager
# config: /etc/ofed/opensm.conf
#
### BEGIN INIT INFO
# Provides:       opensm
# Default-Stop: 0 1 2 3 4 5 6
# Required-Start: rdma
# Required-Stop: rdma
# Short-Description: Starts/Stops the InfiniBand Subnet Manager
# Description: Starts/Stops the InfiniBand Subnet Manager
### END INIT INFO

. /etc/rc.d/init.d/functions

prog=/usr/sbin/opensm
PID_FILE=/var/run/opensm.pid
[ -f /etc/sysconfig/opensm ] && . /etc/sysconfig/opensm

[ -n "$PRIORITY" ] && prio="-p $PRIORITY"

ACTION=$1

start()
{
    local OSM_PID=
    if [ -z "$GUIDS" ]; then
        CONFIGS=""
        CONFIG_CNT=0
        for conf in /etc/rdma/opensm.conf.[0-9]*; do
            CONFIGS="$CONFIGS $conf"
            let CONFIG_CNT++
        done
    else
        GUID_CNT=0
        for guid in $GUIDS; do
            let GUID_CNT++
        done
    fi
    [ -f /var/lock/subsys/opensm ] && return 0
    # Start opensm
    echo -n "Starting IB Subnet Manager: "
    [ -n "$PRIORITY" ] && echo -n "Priority=$PRIORITY "
    [ -n "$GUIDS" ] && echo -n "$GUID_CNT guids "
    [ -n "$CONFIGS" ] && echo -n "$CONFIG_CNT instances "
    if [ -n "$GUIDS" ]; then
	SUBNET_COUNT=0
        for guid in $GUIDS; do
	    SUBNET_PREFIX=`printf "0xfe800000000000%02d" $SUBNET_COUNT`
            $prog -B $prio -g $guid --subnet_prefix $SUBNET_PREFIX >/dev/null 2>&1
	    let SUBNET_COUNT++
        done
    elif [ -n "$CONFIGS" ]; then
        for config in $CONFIGS; do
            $prog -B $prio -F $config >/dev/null 2>&1
        done
    else
        $prog -B $prio >/dev/null 2>&1
    fi
    sleep 20
    OSM_PID=`pidof $prog`
    checkpid $OSM_PID
    RC=$?
    [ $RC -eq 0 ] && echo_success || echo_failure
    [ $RC -eq 0 ] && touch /var/lock/subsys/opensm
    [ $RC -eq 0 ] && echo $OSM_PID > $PID_FILE
    echo
    return $RC    
}

stop()
{
    [ -f /var/lock/subsys/opensm ] || return 0

    echo -n "Stopping IB Subnet Manager(s)."

    OSM_PID=`cat $PID_FILE`

    checkpid $OSM_PID
    RC=$?
    if [ $RC -ne 0 ]; then
	rm -f $PID_FILE
	rm -f /var/lock/subsys/opensm
	echo_success
	return 0
    fi
    # Kill opensm
    kill -15 $OSM_PID >/dev/null 2>&1
    cnt=0
    while [ $cnt -lt 6 ]; do
        checkpid $OSM_PID
	if [ $? -ne 0 ]; then
	    break
	fi
        echo -n "."
	sleep 1
	let cnt++
    done

    checkpid $OSM_PID
    if [ $? -eq 0 ]; then
	kill -KILL $OSM_PID > /dev/null 2>&1
	echo -n "+"
        sleep 1
    fi
    checkpid $OSM_PID
    DEAD=$?
    if [ $DEAD -eq 0 ]; then
	echo_failure
	echo
	return 1
    fi
    echo_success 
    echo
 
    # Remove pid file if any.
    rm -f $PID_FILE
    rm -f /var/lock/subsys/opensm
    return 0    
}

restart ()
{
	stop
	start
}

condrestart ()
{
	[ -f /var/lock/subsys/opensm ] && restart || return 0
}

reload ()
{
	[ -f $PID_FILE ] || return 0
	OSM_PID=`cat $PID_FILE`
	action $"Rescanning IB Subnet:" kill -HUP $OSM_PID
	return $?
}

usage ()
{
	echo
	echo "Usage: `basename $0` {start|stop|restart|condrestart|try-restart|force-reload|status}"
	echo
	return 2
}

case $ACTION in
	start|stop|restart|reload|condrestart|try-restart|force-reload)
	    [ `id -u` != "0" ] && exit 4 ;;
esac

case $ACTION in
	start) start; RC=$? ;;
	stop) stop; RC=$? ;;
	restart) restart; RC=$? ;;
	reload) reload; RC=$? ;;
	condrestart) condrestart; RC=$? ;;
	try-restart) condrestart; RC=$? ;;
	force-reload) condrestart; RC=$? ;;
	status) status $prog; RC=$? ;;
	*) usage; RC=$? ;;
esac

exit $RC
